$(function(){
	parent.IndexPage.toggleLocker('close'); // 沉浸式体验
	$("#mtabset").fixedTabset({
	    'onFirstTabClick':function(target) {
	        switch(target) {
	        // 转JavaBean
	        case 'toJavaBean':
	            // loanInfoDatatable.refresh();
	            break;
	        // 转Mybatis-ResultMap
	        case 'toResultMapFrmContainer':
	            // callLogDatatable.refresh();
	            break;
	        // 转JSP的DataSet
	        case 'toDataSet':
	            // telCustSaleResultDatatable.refresh();
	            break;
	        }
	    }
	});
});

var DevelopmentToolPage = {
	// 删除表实例
	'deleteTable': function(item, e, rowNumber) {
		MsgBox.confirm("确认删除表【" + item.enTableName + "】吗？", null, function() {
			deleteTableStructureByIdAjax.tempUrl(function(url) {
				return url + '/' + item.id;
			}).send();
		});
	},
	// 删除成功
	'deleteTableStructureSucc': function(data, status, xhr) {
		$.messager.info(data.message);
		userTableDatatable.refresh();
	},
	// 变更表实例
	'updateTable': function(item, e, rowNumber) {
		var tableId = item.id;
		MsgBox.open("updateTable", "修改表实例", ctx + '/datum/development/editOne/' + tableId + '/index', '460px');
	},
	// 某项被选中时
	'onUserTableItemClick': function(index, item) {
		$("#tableInfoFrm").autofill(item);
		$("#tableFieldInfoFrm").autofill({
			'tableId': item.id
		});
		findTableFieldAjax.send();
	},
	// 新增表
	'addTable': function() {
		MsgBox.open("addTable", "新增表实例", ctx + '/datum/development/addOne/index', '460px');
	},
	// 条件查询
	'queryByCondition': function() {
		userTableDatatable.refresh();
	},
	'afterReady': function() {
		DevelopmentToolPage.clearDetail();
	},
	// 清理右侧详情页
	'clearDetail': function() {
		$("#tableInfoFrm").autofill({});
		$("#tableFieldInfoFrm").find("input[name='tableId']").val('10');
		findTableFieldAjax.send();
	},
	// 删除字段
	'deleteField': function(item, e, rowNumber) {
		MsgBox.confirm("确认删除字段【" + item.fieldName + "】吗？", null, function() {
			deleteFieldAjax.tempUrl(function(url) {
				return url + '/' + item.id;
			}).send();
		});
	},
	// 删除字段成功回调
	'onDeleteFieldSucc': function(data, status, xhr) {
		$.messager.info(data.message);
		tableFieldDatatable.refresh();
	},
	// 修改
	'updateField': function(item, index) {
		var curr = userTableDatatable.getCurrentItem();
		if (curr == null) {
			$.messager.info("请从左侧表格中选择一条记录！");
			return ;
		}
		MsgBox.open("updateField", "更新字段", ctx + "/datum/development/editField/" + curr.id + '/' + item.id + '/index', "460px");
	},
	// 新增字段
	'addField': function() {
		var curr = userTableDatatable.getCurrentItem();
		if (curr == null) {
			$.messager.info("请从左侧表格中选择一条记录！");
			return ;
		}
		MsgBox.open("addField", "新增字段", ctx + "/datum/development/addField/" + curr.id + '/index', "460px");
	},
	// 查询字段列表
	'queryFields': function() {
		tableFieldDatatable.refresh();
	},
	// 查看变更历史
	'viewHistory': function() {
		var curr = userTableDatatable.getCurrentItem();
		if (curr == null) {
			$.messager.info("请从左侧表格中选择一条记录！");
			return ;
		}
		MsgBox.open("viewHistory", "查看变更历史", ctx + "/datum/development/viewHistory/" + curr.id + '/index', "560px");
	},
	'handleRowData': function(text, item) {
		return item.cnProjectName + ' --> ' + text; 
	},
	'trans': function(ajax) {
		var curr = userTableDatatable.getCurrentItem();
		if (curr == null) {
			$.messager.info("请从左侧表格中选择一条记录！");
			return ;
		}
		ajax.tempUrl(function(url) {
			return url + curr.id;
		}).send();
	},
	// 转javaBean
	'transferToJavaBean':function() {
		DevelopmentToolPage.trans(transToJavaBeanAjax);
	},
	// 转ResultMap
	'transToResultMap': function() {
		DevelopmentToolPage.trans(transToResultMapAjax);
	},
	// 转JavaMapper
	'transToJavaMapper': function() {
		DevelopmentToolPage.trans(transToJavaMapperAjax);
	},
	// 转DataSet
	'transToDataSet': function() {
		DevelopmentToolPage.trans(transToDataSetAjax);
	},
	// 转建表SQL
	'transToCreateSQL': function() {
		DevelopmentToolPage.trans(transToCreateSQLAjax);
	},
	'onTransToJavaBeanSucc': function(data, status, xhr) {
		$.messager.info(data.message);
		$("#toJavaBeanFrm").autofill(data.info);
	},
	'onTransToResultMapSucc': function(data, status, xhr) {
		$.messager.info(data.message);
		$("#toResultMapFrm").autofill(data.info, {
			'htmlDecode': false,
			'clearForm': false
		});
	},
	'onTransToJavaMapperSucc': function(data, status, xhr) {
		$.messager.info(data.message);
		$("#toJavaMapperFrm").autofill(data.info, {
			'htmlDecode': false
		});
	},
	'onTransToDataSetSucc': function(data, status, xhr) {
		$.messager.info(data.message);
		$("#toDataSetFrm").autofill(data.info, {
			'htmlDecode': false
		});
	},
	'onTransToCreateSQLSucc': function(data, status, xhr) {
		$.messager.info(data.message);
		$("#toCreateSQLFrm").autofill(data.info, {
			'htmlDecode': false,
			'clearForm': false
		});
	},
	'copy': function($frm) {
		$frm.find("textarea[name='result']")[0].select();
		try {
			var successful = document.execCommand('copy');
			var msg = successful ? '成功复制到剪贴板' : '该浏览器不支持点击复制到剪贴板';
			$.messager.info(msg);
		} catch (err) {
			$.messager.info('该浏览器不支持点击复制到剪贴板');
		}
	},
	'copyJavaBean': function() {
		DevelopmentToolPage.copy($("#toJavaBeanFrm"));
	},
	'copyResultMap': function() {
		DevelopmentToolPage.copy($("#toResultMapFrm"));
	},
	'copyJavaMapper': function() {
		DevelopmentToolPage.copy($("#toJavaMapperFrm"));
	},
	'copyToDataSet': function() {
		DevelopmentToolPage.copy($("#toDataSetFrm"));
	},
	'copyCreateSQL': function() {
		DevelopmentToolPage.copy($("#toCreateSQLFrm"));
	},
	'onRenderItem': function(colName,text,indexStr,mItem,rowNumber) {
		 switch(colName) {
		    case 'doQuery':
		    case 'queryMode':
		    case 'doUpdate':
		    case 'allowNull':
		    case 'isPrimaryKey':
		    	return $.buildDatatableCheckbox({
		    		'name': colName,
		    		'rowNumber': rowNumber,
		    		'id': mItem.id,
		    		'checked': (text == '1'),
		    		'onValueChanged': ({
		    			'doQuery': DevelopmentToolPage.onDoQueryValueChanged,
		    		    'queryMode': DevelopmentToolPage.onQueryModeValueChanged,
		    		    'doUpdate': DevelopmentToolPage.onDoUpdateValueChanged,
		    		    'allowNull': DevelopmentToolPage.onAllowNullValueChanged,
		    		    'isPrimaryKey': DevelopmentToolPage.onIsPrimaryKeyChanged
	    			})[colName]
		    	});
		    case 'display':
		    	return $.buildDatatableInput({
		    		'name': 'display',
		    		'rowNumber': rowNumber,
		    		'id': mItem.id,
		    		'okFunc': DevelopmentToolPage.updateDisplay,
		    		'text': text,
		    		'width': '80px'
		    	});
		    case 'comment':
		    	return $.buildDatatableInput({
		    		'name': 'comment',
		    		'rowNumber': rowNumber,
		    		'id': mItem.id,
		    		'okFunc': DevelopmentToolPage.updateComment,
		    		'text': text,
		    		'width': '95px'
		    	});
	    }
	},
	'onDoQueryValueChanged': function(id, checked) {
		var value = checked ? '1': '0';
		updateDoQueryAjax.tempUrl(function(url) {
			return url + id + '/' + value;
		}).send();
	},
	'onQueryModeValueChanged': function(id, checked) {
		var value = checked ? '1': '0';
		updateQueryModeAjax.tempUrl(function(url) {
			return url + id + '/' + value;
		}).send();
	},
	'onDoUpdateValueChanged': function(id, checked) {
		var value = checked ? '1': '0';
		updateDoUpdateAjax.tempUrl(function(url) {
			return url + id + '/' + value;
		}).send();
	},
	'onUpdateSuccess':function(data, status, xhr) {
		$.messager.info(data.message);
	},
	'selectAll': function(ele) {
		var selectedVal = $(ele).prop("checked");
		$("#toResultMapFrm").find("input[data-cbelement]").each(function() {
			$(this).prop("checked", selectedVal);
		});
	},
	'stateChanged': function(ele) {
		var selectedVal = $(ele).prop("checked");
		if (!selectedVal) {
			$("#toResultMapFrm").find("input[name='selectAll']").prop("checked", false);
		}
	},
	'openBatchAddDialog': function() {
		var curr = userTableDatatable.getCurrentItem();
		if (curr == null) {
			$.messager.info("请从左侧表格中选择一条记录！");
			return ;
		}
		MsgBox.open("openBatchAddDialog", "批量新增数据库字段", ctx + "/datum/development/field/batchAdd/" + curr.id + "/index", "680px");
	},
	'onAllowNullValueChanged':  function(id, checked) {
		var value = checked ? '1': '0';
		updateAllNullAjax.tempUrl(function(url) {
			return url + id + '/' + value;
		}).send();
	},
	'onIsPrimaryKeyChanged':  function(id, checked) {
		var value = checked ? '1': '0';
		updateIsPrimaryKeyAjax.tempUrl(function(url) {
			return url + id + '/' + value;
		}).send();
	},
	'updateDisplay': function(id, value) {
		// 更新display
		updateDisplayAjax.tempUrl(function(url) {
			return url + id;
		}).setData({
			'display': value
		}).send();
	},
	'updateComment': function(id, value) {
		// 更新comment
		updateCommentAjax.tempUrl(function(url) {
			return url + id;
		}).setData({
			'comment': value
		}).send();
	}
};